package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"yukao1/pkg"
)

func UploadFile(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	if file.Size > 1024*1024*3 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "文件过大",
		})
		return
	}

	dst := "C:\\Users\\a\\Desktop\\yukao1\\static\\img/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	//oss 上传
	ossUrl := pkg.FileOss(dst, file.Filename)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  fmt.Sprintf("'%s' uploaded!", file.Filename),
		"url":  ossUrl,
	})
}
